﻿// RegisterDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "RegisterDlg.h"
#include "afxdialogex.h"
#include "resource.h"
#include <string>
using namespace std;


// RegisterDlg 对话框

IMPLEMENT_DYNAMIC(RegisterDlg, CDialogEx)

RegisterDlg::RegisterDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(DLG_REGISTER, pParent)
{
	pRegisterDB = nullptr;
}

RegisterDlg::~RegisterDlg()
{
}

void RegisterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, EDT_REGISTER_NAME, mRegisterName);
	DDX_Control(pDX, EDT_REGISTER_PASSWORD, mRegisterPwd);
	DDX_Control(pDX, IDC_CHECK1, mIsAdmin);
	DDX_Control(pDX, IDC_EDIT1, mAutoCode);
}


BEGIN_MESSAGE_MAP(RegisterDlg, CDialogEx)
	ON_BN_CLICKED(BTN_TO_REGISTER, &RegisterDlg::OnBnClickedToRegister)
	ON_BN_CLICKED(BTN_CANCEL, &RegisterDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


void RegisterDlg::OnBnClickedToRegister()
{
	int isAdmin = -1;
	CString csName;
	mRegisterName.GetWindowTextW(csName);
	string strName = CT2A(csName.GetBuffer());
	CString csPwd;
	mRegisterPwd.GetWindowTextW(csPwd);
	string strPwd = CT2A(csPwd.GetBuffer());
	CString csAuthCode;
	mAutoCode.GetWindowTextW(csAuthCode);
	string strAuthCode = CT2A(csAuthCode.GetBuffer());

	isAdmin = mIsAdmin.GetCheck();
	string strAuthRight = "tester";
	if (isAdmin)
	{
		strAuthRight = "admin";
	}

	if (strName == "" || strAuthCode == "" || strPwd == "")
	{
		AfxMessageBox(L"请填写完整");
		return;
	}
	string rootPwd = pRegisterDB->getRootPwd();
	if (rootPwd == strAuthCode && rootPwd != "")
	{
		if (pRegisterDB->isUserExist(strName))
		{
			AfxMessageBox(L"用户名重复，无法创建用户！");
		}
		else
		{
			pRegisterDB->addAccount(strName, strPwd, strAuthRight);
			CDialogEx::OnOK();
		}

	}
	else
	{
		AfxMessageBox(L"授权码错误！无法创建用户。");
	}



}


void RegisterDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}
